#!/usr/bin/perl
# Copyright (C) 2006, 2007  Stephane Alnet
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information visit http://carrierclass.net/
#

package CCNCore::spool::distribute_keys;
use strict; use warnings;
use CCNCore::IPC::Ticket;
use CCNCore::Provisioning::Local;
use CCNCore::Provision::distribute_keys;

# This must be received on a Manager node.
# $server_pk    Optional: server PK to distribute keys to.

our $D;

sub run
{
    my $ticket   = pop;
    my $callback = pop;

    my $server_pk = shift; # Optional

    # Create a ticket to monitor the progress of the remote hosts.
    my $t = new CCNCore::IPC::Ticket;
    $t->set_label('Distributing keys'.(defined $server_pk?" to server $server_pk":''));
    $t->add_context('callback',$callback);
    $t->add_context('ticket',$ticket);

    # Proceed with the keys distribution
    my $p = new CCNCore::Provisioning::Local;
    my $n = new CCNCore::Provision::distribute_keys($p);
    $n->commit($t,$server_pk);

    $D->log('success');
}
1;